﻿/**
 * ...
 * @author sminutoli
 */

import com.dmotiko.general.*
import com.dmotiko.lib.TimerMovie;
import com.dmotiko.prune.*
import mx.transitions.easing.Strong;
import mx.transitions.Tween;

class NavBar 
extends SiteBar
{
	
	private var background:BaseClip;
	private var timer:MovieClip;
		
	private function initClip():Void {
		super.initClip();
		
		timer = TimerMovie.createTimer(this);
				
		background = BaseClip( attachMovie("NavBarBack", "background", this.getNextHighestDepth()) );
		background._y = -123;
				
	}
	
	private function section_changed( e ):Void 
	{
		if ( e.section == SiteSections.PRUNE || e.section == SiteSections.CONTACT || e.section == SiteSections.BELONG ) {
			btn_active.setActive(false, true );
			return;
		}
		
		var btn:ButtonClip = getBtnByValue( e.section );
		
		if ( btn && btn != btn_active ) {
			btn_active.setActive( false, true );
			btn_active = btn;
			if ( btn_active._currentframe == 1 ) btn_active.onRollOver();
			btn_active.setActive( true, true );
		} 
	}
		
	private function createBtns():Void {
		super.createBtns(false);
		container._x = 23;
		dispatchEvent( { type: BaseClipEvent.READY, target: this } );
		
	}
	
	private function btn_changed( e ):Void 
	{
		Site.getApp().setSection( e.data.value );
	}
		
	public function show():Void
	{
		
		background.show();
		
		var bar:BaseClip;
		switch ( Site.getApp().getSection() ) 
		{
			case SiteSections.CAMPAIGN:
			bar = Site.getApp().getClips().getCampaignBar();
			break;
			
			case SiteSections.COLLECTION:
			bar = Site.getApp().getClips().getCollectionBar();
			break;
			
			case SiteSections.STORES:
			bar = Site.getApp().getClips().getStoresBar();
			break;
			
			case SiteSections.FASHION:
			bar = Site.getApp().getClips().getFashionBar();
			break;
		}
				
		if ( bar ) {
			registerTween( "moveCampaignBar", new Tween( bar, "_y", Strong.easeOut, bar._y, 325, 0.7, true) );
			timer.startTimer( { bind:this, listener: timer_subBar, delay: 50 } );
		} else {
			super.show();
		}
	}
	
	public function hide():Void
	{
		super.hide();
		
		background.hide();
		
		var bar:BaseClip;
		switch ( Site.getApp().getSection() ) 
		{
			case SiteSections.CAMPAIGN:
			bar = Site.getApp().getClips().getCampaignBar();
			break;
			
			case SiteSections.COLLECTION:
			bar = Site.getApp().getClips().getCollectionBar();
			break;
			
			case SiteSections.STORES:
			bar = Site.getApp().getClips().getStoresBar();
			break;
			
			case SiteSections.FASHION:
			bar = Site.getApp().getClips().getFashionBar();
			break;
		}
				
		if ( bar ) {
			registerTween( "moveCampaignBar", new Tween( bar, "_y", Strong.easeInOut, bar._y, 122, 0.7, true) );
		}
	}
	
	public function onMouseMove():Void {
		if ( bShowing ) return;
				
		if ( bShowed && _xmouse > this._width ) {
			hide();
		
		} else if( !bShowed && _xmouse < this._width && _ymouse < getBounds(_parent).yMax ) {
			show();
		
		}
	}
	
	private function tweenFinished( key:String, tween:Tween):Void {
		if ( key.indexOf("fadeBtn") > -1 && btns[0]._alpha > 0 ) {
			bShowing = false;
			timer.startTimer( { bind:this, listener: timer_complete, delay: 2000 } );
			showEnd();
						
		} else if ( key.indexOf("fadeBtn") > -1 && btns[0]._alpha == 0 ) {
			bShowing = false;
			for ( var btn in btns ) { btn._visible = false };			
			hideEnd();
			
		}
	}
	
	private function timer_subBar():Void {
		super.show();
	}
	
	private function timer_complete():Void
	{
		if( bShowed && _xmouse > this._width && _ymouse > getBounds(_parent).yMax ) hide();
	}
	
}